<?php
class User_model extends CI_Model{
	
	var $table = "users";
	var $table_address_book = "address_book";
    public $memcache;
    
	function __construct(){
		parent::__construct();
        $this->load->driver('cache', array('adapter' => 'memcached'));        
	}
    
    public function insertUser( $data ){        
        $this->db->set($data);
        $this->db->insert( $this->table );
        return $this->db->insert_id();
    }
    
    public function getUser( $id ){
        $where = array('id' => $id);
        $this->db->select('id' , 'username' , 'email' , 'avatar' , 'created' , 'status')->from($this->table)->where($where);
        return $this->db->get();         
    }
    
     public function get_user_by_array( $id , $array , $type){
        $where = array($type => $id);
        $this->db->select($array)->from($this->table)->where($where);
        return $this->db->get()->row();         
    }
    
    public function checkLogin($email , $password){  
        $where = array('email' => $email , 'password' => $password);
        $this->db->select('id' , 'count(id) as total')->from($this->table)->where($where);
        return $this->db->get()->row()->id; 
    }
    
    public function insertRole( $data ){
        $this->db->set($data);
        $this->db->insert( 'users_roles' );
        return $this->db->insert_id();
    }
    /**
     * 
     * @param type $email
     * @param type $status
     * @return type
     * @return TRUE IS have
     */
    public function checkHaveEmail( $email ){
        $where = array('email' => $email);
        $this->db->select('id')->from($this->table)->where($where);
        $num_rows = $this->db->get()->num_rows; 
        return $num_rows ? TRUE : FALSE;        
    }
    
    public function get_user_by_email( $email ){
        $query = $this->db->select('username , email, id, password, active, last_login , full_name , avatar')
		                  ->where('email', $this->db->escape_str($email))
		                  ->limit(1)
		                  ->get($this->table);
        return $query->row();        
    }
    
    public function check_address_book($uid){
        $where = array('uid' => $uid);
        $this->db->select('id')->from($this->table_address_book)->where($where);
        $num_rows = $this->db->get()->num_rows; 
        return $num_rows ? TRUE : FALSE;        
    }
    
    public function insert_address_book($data){
        $this->db->set($data);
        $this->db->insert( $this->table_address_book );
        return $this->db->insert_id();
    }
    
    public function update_address_book($uid , $data){        
        $where = array('uid' => $uid);
        $this->db->where($where);
        $update = $this->db->update($this->table_address_book, $data);
        return $update ? true : false;        
    }
    
    public function get_address_book_by_uid( $uid ){
        $query = "SELECT * FROM $this->table_address_book WHERE uid = $uid";
        $key = md5($query);       
        
        $result = $this->cache->memcached->get($key); //die(print_r($result));
        if($result == null){            
            $query_results = $this->db->query($query);   
            $this->cache->memcached->save($key, $query_results->row_array(), 100);
            return $query_results->row_array();
        }else {            
            return $result;
        }
    }
}